package com.codeying.utils.component;

import com.alibaba.fastjson.JSON;
import java.io.Serializable;

/**
* 用于和前端交互消息
 * ajax请求返回内容
 * success表示成功与否
 * message是提示消息
 * data是数据
 * code是状态码
 */
public class ApiResult<T> implements Serializable {

    private int code;

    private Boolean success;

    private String message;

    private T data;

    public ApiResult(Integer code, Boolean success) {
        this.code = code;
        this.success = success;
    }

    public ApiResult(Integer code, Boolean success, String message) {
        this.code = code;
        this.success = success;
        this.message = message;
    }

    public ApiResult(Integer code, Boolean success, String message, T data) {
        this.code = code;
        this.success = success;
        this.message = message;
        this.data = data;
    }

    public static String success() {
        return JSON.toJSONString(new ApiResult<>(200, true,"操作成功"));
    }

    public static String successMsg(String message) {
        return JSON.toJSONString(new ApiResult<>(200, true, message));
    }

    public static <T> String successData(T data) {
        return JSON.toJSONString(new ApiResult<>(200, true, "操作成功", data));
    }

    public static <T> String successMsgAndData(String message, T data) {
        return JSON.toJSONString(new ApiResult<>(200, true, message, data));
    }

    public static String fail() {
        return JSON.toJSONString(new ApiResult<>(500, false, "操作失败"));
    }

    public static String fail(String message) {
        return JSON.toJSONString(new ApiResult<>(500, false, message, null));
    }

    public static <T> String fail(String message , T data){
        return JSON.toJSONString(new ApiResult<>(500,false,message,data));
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }



}

